// 当管理员选择图片的时候
$('#file').on('change', function () {
    let file = this.files[0];
    let formData = new FormData();
    formData.append('image', file);
    $.ajax({
        type: 'post',
        url: '/upload',
        data: formData,
        processData: false,
        contentType: false,
        success: function (response) {
            $('#image').val(response[0].image);
        }
    })
});

// 添加轮播图片
$('#slidesForm').on('submit', function () {
    let formData = $(this).serialize();
    $.ajax({
        type: 'post',
        url: '/slides',
        data: formData,
        success: function () {
            location.reload();
        }
    })
    return false;
});

// 获取轮播图列表
$.ajax({
    type: 'get',
    url: '/slides',
    success: function (response) {
        let html = template('slidesTpl', {
            data: response
        });
        $('#slidesBox').html(html);
    }
});

// 删除轮播图片
$('#slidesBox').on('click', '.delete', function () {
    if (confirm('您真的要把这个轮播图片删除掉吗？')) {
        let id = $(this).attr('data-id');
        $.ajax({
            type: 'delete',
            url: '/slides/' + id,
            success: function () {
                location.reload();
            }
        })
    }
})